package com.jackiechancinema.ssm.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.AdminDao;
import com.jackiechancinema.ssm.dto.AdminExcelDto;
import com.jackiechancinema.ssm.entity.Admin;
import com.jackiechancinema.ssm.utils.BeanUtil;
import com.jackiechancinema.ssm.utils.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class AdminReadListener extends AnalysisEventListener<AdminExcelDto> {
    @Autowired
    private AdminDao dao;

    private List<Admin> list=new ArrayList<Admin>();
    //获取读取到的每一行对应的对象
    @Override
    public void invoke(AdminExcelDto dto, AnalysisContext analysisContext) {
        //实现属性的赋值
        Admin admin= BeanUtil.copyProperty(Admin.class,dto,dto.getClass().getDeclaredFields());
        //密码转为密文
        admin.setAdmin_password(EncryptUtil.aesenc(SystemContant.PASS_KEY,dto.getAdmin_password()));
        //添加到集合
        list.add(admin);
    }
    //整个读取结束
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //将读取的数据一次性的存储到数据库
       int r=dao.insertBatch(list);
       list.clear();
    }
}